import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {HttpClient} from '@angular/common/http';

/**
 * Generated class for the OwnerUploadImgPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-owner-upload-img',
  templateUrl: 'owner-upload-img.html',
})
export class OwnerUploadImgPage {
  houseId:any;
  imgIndex:number = 0;
  previewImgs=[];
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private http:HttpClient,

  ) {
  }

  ionViewDidLoad() {
    let that = this;
    that.houseId=this.navParams.get('houseId');
  }
  onFileChanged(fileList: FileList) {
  if(this.imgIndex<6){
    if (fileList.length > 0) {
      let that = this;
      let file: File = fileList[0];
      // that.preview(file);
      let formData: FormData = new FormData();
      formData.append('file', file, file.name);
      formData.append('key', 'images/houses/'+that.houseId+'/'+that.imgIndex +'.jpg');
      formData.append('token', 'Ulg_mgLt7ByWKL4h2_fF1Gt80YShyr3a89lHaUnM:5T42rjQDg-qxCDncqf4XnTzt0K8=:eyJzY29wZSI6ImFuamlhIiwiZGVhZGxpbmUiOjE1MTQ2MDQ0NTk3ODh9');
      let headers = new Headers({
        "Accept": "application/json"
      });
      // let options = new RequestOptions({ headers });

      let Imgsrc = "images/houses/"+that.houseId+"/"+that.imgIndex++ +".jpg";
      that.previewImgs.push(Imgsrc);
      console.log(that.previewImgs);
      this.http.post("http://up.qiniu.com/", formData)
        .subscribe(
          function (result) {


            // that.imgIndex++;
            // $('#preview').empty();
            // console.log(that.shenfen)
          },
          function (error) {
            console.log(error.message);
          }
        )
    }
  }

}

}
